package com.cyjt.operation.fragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.cyjt.operation.R;
import com.cyjt.operation.adapter.StaticNodesStatusAdapter;
import com.cyjt.operation.base.AppContext;
import com.cyjt.operation.base.HandlerMessageCodes;
import com.cyjt.operation.bean.StaticNodesStatusInfo;
import com.cyjt.operation.ui.NodesStatusListActivity;
import com.cyjt.operation.view.ActionBarLayout;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 显示静态节点状态列表的Fragment，附属于NodesStatusListActivity的ViewPager
 * 
 * @author LTP
 */
public class StaticNodesStatusFragment extends Fragment {
	private View view;
	/** 显示静态节点状态列表的ListView */
	private ListView listView_static_nodes_status_list;
	/** 下拉刷新控件 */
	public static SwipeRefreshLayout mSwipeRefreshLayout;
	/** 当静态节点状态列表为空的提示 */
	private TextView textView_static_nodes_status_hint;
	/** 全部状态下的静态节点的列表 */
	private List<StaticNodesStatusInfo> staticNodesStatusInfoList;
	/** 正常状态下的静态节点的列表 */
	private List<StaticNodesStatusInfo> nomalStaticNodesStatusInfolList = new ArrayList<StaticNodesStatusInfo>();
	/** 异常状态下的静态节点的列表 */
	private List<StaticNodesStatusInfo> abnomalStaticNodesStatusInfolList = new ArrayList<StaticNodesStatusInfo>();
	private StaticNodesStatusAdapter mStaticNodesStatusAdapter;

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HandlerMessageCodes.FETCH_STATIC_NODES_STATUS_LIST_SUCCEED:
				ActionBarLayout.isShowProgressBar(false);// 加载成功时，使标题上的进度圈不显示
				if (staticNodesStatusInfoList != null) {
					staticNodesStatusInfoList.clear();
				}
				nomalStaticNodesStatusInfolList.clear();
				abnomalStaticNodesStatusInfolList.clear();
				staticNodesStatusInfoList = (List<StaticNodesStatusInfo>) msg.obj;
				separateStaticNodeListData();
				if ("查看正常节点".equals(NodesStatusListActivity.getButtonText())) {
					loadAbnormalStaticNodesStatusListData();
				} else {
					loadNomalStaticNodesStatusList();
				}
				break;
			case HandlerMessageCodes.FETCH_STATIC_NODES_STATUS_LIST_FAILED:
				ActionBarLayout.isShowProgressBar(false);
				Toast.makeText(getActivity(), "从服务器获取的静态节点信息为空", Toast.LENGTH_SHORT).show();
				break;

			default:
				break;
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_static_node_status, container, false);
		initView();
		feachStaticNodesStatus();// 从服务器获取静态节点状态的数据
		return view;
	}

	/**
	 * 从服务器获取静态节点状态的数据
	 */
	public void feachStaticNodesStatus() {
		AppContext.getInstance().getVolleyTools().fetchStaticNodesStatusList(handler,
				AppContext.getInstance().getUser());
	}

	/**
	 * 控件初始化
	 */
	private void initView() {
		ActionBarLayout.isShowProgressBar(true);
		textView_static_nodes_status_hint = (TextView) view.findViewById(R.id.textView_static_nodes_status_hint);
		listView_static_nodes_status_list = (ListView) view.findViewById(R.id.listView_static_nodes_status_list);
		listView_static_nodes_status_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO 静态节点列表ListView的点击事件
				DialogFragmentForDeviceStatusDetails mDeviceStatusDetails = new DialogFragmentForDeviceStatusDetails();
				mDeviceStatusDetails.setStyle(DialogFragment.STYLE_NO_TITLE,
						android.R.style.Theme_Holo_Dialog_NoActionBar_MinWidth);
				Bundle args = new Bundle();
				StaticNodesStatusInfo staticNodesStatusInfo;
				if ("查看正常节点".equals(NodesStatusListActivity.getButtonText())) {
					staticNodesStatusInfo = abnomalStaticNodesStatusInfolList.get(position);
				} else {
					staticNodesStatusInfo = nomalStaticNodesStatusInfolList.get(position);
				}
				String staticNodesStatusInfoString = "车位编码：" + staticNodesStatusInfo.getCode() + "\n\n节点编号："
						+ staticNodesStatusInfo.getSensorCode() + "\n\n基站编码："
						+ staticNodesStatusInfo.getBasestationNfcCode() + "\n\n所在路段：" + staticNodesStatusInfo.getTitle()
						+ "\n\n最新心跳：" + staticNodesStatusInfo.getSubmitAtString() + "\n\n节点状态："
						+ staticNodesStatusInfo.getStatusToString() + "\n\n车位状态："
						+ staticNodesStatusInfo.getStateToString();
				// args.putString("title", "静态节点详情");
				args.putString("text", staticNodesStatusInfoString);
				mDeviceStatusDetails.setArguments(args);
				mDeviceStatusDetails.show(getActivity().getFragmentManager(), "nodesDetailInfo");
			}
		});
		mSwipeRefreshLayout = (SwipeRefreshLayout) view
				.findViewById(R.id.swiprrefreshLayout_for_static_nodes_status_list);
		mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_red_dark, android.R.color.holo_green_dark,
				android.R.color.holo_blue_dark, android.R.color.holo_purple);
		mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				ActionBarLayout.isShowProgressBar(true);
				feachStaticNodesStatus();
				mSwipeRefreshLayout.setRefreshing(false);
			}
		});
	}

	/**
	 * 分离静态节点列表中正异常状态的数据
	 */
	private void separateStaticNodeListData() {
		// 把静态节点按照节点编码大小排序
		Collections.sort(staticNodesStatusInfoList);
		for (StaticNodesStatusInfo stiticNodesStatusInfo : staticNodesStatusInfoList) {
			if (stiticNodesStatusInfo.getStatus() == 1) {
				// 当静态节点的状态为1即正常时，装载进normalNodesStatusInfoList
				nomalStaticNodesStatusInfolList.add(stiticNodesStatusInfo);
			} else if (stiticNodesStatusInfo.getStatus() == 2) {
				// 当静态节点的状态为2即异常时，装载进abnormalNodesStatusInfoList
				abnomalStaticNodesStatusInfolList.add(stiticNodesStatusInfo);
			}
		}
	}

	/**
	 * 加载异常静态节点的列表数据
	 */
	public void loadAbnormalStaticNodesStatusListData() {
		textView_static_nodes_status_hint.setText("");
		if (abnomalStaticNodesStatusInfolList.isEmpty()) {
			textView_static_nodes_status_hint.setText("异常静态节点列表为空");
		}
		mStaticNodesStatusAdapter = new StaticNodesStatusAdapter(getActivity(), abnomalStaticNodesStatusInfolList);
		listView_static_nodes_status_list.setAdapter(mStaticNodesStatusAdapter);
	}

	/**
	 * 加载正常静态节点的列表数据
	 */
	public void loadNomalStaticNodesStatusList() {
		textView_static_nodes_status_hint.setText("");
		if (nomalStaticNodesStatusInfolList.isEmpty()) {
			textView_static_nodes_status_hint.setText("正常静态节点列表为空");
		}
		mStaticNodesStatusAdapter = new StaticNodesStatusAdapter(getActivity(), nomalStaticNodesStatusInfolList);
		listView_static_nodes_status_list.setAdapter(mStaticNodesStatusAdapter);
	}

}
